/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.server;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import ru.ssau.model.DataModel;
import ru.ssau.utils.XMLHelper;

/**
 *
 * @author Дима
 */
public class ServerFrame extends JFrame {

    private JButton connectButton;
    private JButton exitButton;
    private JTextArea infoArea;
    private ConnectionDialog connectionDialog;
    private JFileChooser chooser;
    
    private Server server;

    private int port;

    public ServerFrame() {
        setTitle("Server");
        initComponents();
        layoutComponents();
        createMenu();

        addWindowListener(new WindowAdapter() {
            public void windowClosing() {
                if(server != null) {
                    server.closeConnection();
                }
                System.exit(0);
            }
        });
    }

    private void initComponents() {
        server = new Server(new DataModel());

        connectionDialog = new ConnectionDialog(this);

        connectButton = new JButton("Connect");
        connectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(connectionDialog.showDialog()) {
                    port = connectionDialog.getPort();
                    //server = new Server(new DataModel());
                    Thread serverThread = new Thread(new Runnable() {
                        public void run(){
                            try {
                                server.startServer(port);
                            } catch (IOException ex) {
                                JOptionPane.showMessageDialog(ServerFrame.this,
                                ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    });
                    serverThread.start();
                    connectButton.setEnabled(false);
                }
            }
        });

        exitButton = new JButton("Exit");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                server.closeConnection();
                System.exit(0);
            }
        });

        infoArea = new JTextArea(8,25);
        infoArea.setLineWrap(true);

        server.addInfoMessageListener(new InfoMessageListener() {
            public void receiveInfoMessage(String infoMessage) {
                infoArea.append(infoMessage + "\n");
            }
        });

    }

    private void layoutComponents() {
        setLayout(new BorderLayout());
        add(new JScrollPane(infoArea), BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        add(buttonPanel, BorderLayout.SOUTH);
        buttonPanel.add(connectButton);
        buttonPanel.add(exitButton);

        pack();
    }
    
    private void createMenu() {
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        
        JMenuItem exportItem = new JMenuItem("Export");
        fileMenu.add(exportItem);
        exportItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(chooser == null) {
                    chooser = new JFileChooser();
                    chooser.setFileFilter(new FileNameExtensionFilter("xml files", "xml"));
                }
                if(chooser.showSaveDialog(ServerFrame.this) == JFileChooser.APPROVE_OPTION) {
                    File file = chooser.getSelectedFile();
                    String fileName = file.getPath();
                    if(!fileName.endsWith(".xml")) {
                        file = new File(fileName + ".xml");
                    }
                    XMLHelper.saveXMLToFile(server.getDataModel().toXMLElement(), file);
                }
            }
        });
    }
}
